package com.muchfish.webflux.api;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.*;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.springframework.web.reactive.socket.server.WebSocketService;

@Configuration
public class MyWebConfiguration {

    /**
     * 自定义WebFlux配置
     * @return
     */
    @Bean
    public WebFluxConfigurer webFluxConfigurer() {
        return new WebFluxConfigurer() {
            @Override
            public void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) {
                // 配置内容类型解析器
                // builder.xxx(...);
            }

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                // 配置全局跨域规则
                registry.addMapping("/api/**")
                        .allowedOrigins("https://example.com")
                        .allowedMethods("GET", "POST");
            }

            @Override
            public void configurePathMatching(PathMatchConfigurer configurer) {
                // 配置路径匹配选项
                // configurer.xxx(...);
            }

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                // 配置静态资源处理器
                registry.addResourceHandler("/static/**")
                        .addResourceLocations("classpath:/static/");
            }

            @Override
            public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
                // 配置自定义参数解析器
                // configurer.xxx(...);
            }

            @Override
            public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
                // 配置HTTP消息编解码器
                // configurer.xxx(...);
            }

            @Override
            public void addFormatters(FormatterRegistry registry) {
                // 添加自定义格式化器
                // registry.xxx(...);
            }

            @Override
            public Validator getValidator() {
                // 提供自定义的验证器
                // return new MyValidator();
                return null;
            }

            @Override
            public MessageCodesResolver getMessageCodesResolver() {
                // 提供自定义的消息码解析器
                // return new MyMessageCodesResolver();
                return null;
            }

            @Override
            public WebSocketService getWebSocketService() {
                // 提供自定义的WebSocket服务
                // return new MyWebSocketService();
                return null;
            }

            @Override
            public void configureViewResolvers(ViewResolverRegistry registry) {
                // 配置视图解析器
                // registry.xxx(...);
            }
        };
    }
}
